﻿using AutoMapper;
using ManagementApi.Models.System.Role;
using Modules.Common;
using Modules.Corp;
using Modules.Device.Entitys;
using Modules.Project;
using Modules.System.Role;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using static Modules.Device.CheckInDeviceConstants;
using Modules.System.User;

namespace ManagementApi.Models.Device
{
    public class CheckInDeviceDto : OperateDto
    {
        /// <summary>
        /// 主键
        ///</summary>
        public int Id { get; set; }

        /// <summary>
        /// 项目id
        ///</summary>
        public int ProjectId { get; set; }

        /// <summary>
        /// 企业Id
        ///</summary>
        public int CorpId { get; set; }

        /// <summary>
        /// 设备类型(海清......)
        ///</summary>
        public 考勤设备设备厂家 Type { get; set; }

        public string TypeName { get; set; }

        /// <summary>
        /// 设备名称
        ///</summary>
        public string Name { get; set; }

        /// <summary>
        /// 设备序列号(设备编号)
        ///</summary>
        public string Code { get; set; }

        /// <summary>
        /// 有效起始日期
        ///</summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 有效截至日期
        ///</summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 进出(1 进  2 出)
        ///</summary>
        public 进出 InOut { get; set; }

        public string InOutName { get; set; }

        /// <summary>
        /// 设备型号
        ///</summary>
        public string Model { get; set; }

        /// <summary>
        /// 设备版本
        ///</summary>
        public string Version { get; set; }

        /// <summary>
        /// 备注
        ///</summary>
        public string Remark { get; set; }

        /// <summary>
        /// 是否禁用
        ///</summary>
        public bool IsDisable { get; set; }

        /// <summary>
        /// 创建时间
        ///</summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 是否在线
        ///</summary>
        public bool IsOnline { get; set; }

        /// <summary>
        /// ip地址
        ///</summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 最后同步时间
        /// </summary>
        public DateTime LastSyncTime { get; set; }

        #region 扩展属性

        public string ProjectName { get; set; }
        public string CorpName { get; set; }

        #endregion 扩展属性
    }

    public static partial class ProjectCheckInDeviceExceptions
    {
        public static CheckInDeviceDto AsDto(this CheckinDevice entity, IApplicationContext applicationContext)
        {
            var mapper = applicationContext.GetService<IMapper>();
            CheckInDeviceDto dto = new();
            mapper.Map(entity, dto);
            dto.TypeName = dto.Type.GetDescription();
            dto.InOutName = dto.InOut.GetDescription();
            var corpService = applicationContext.GetService<CorpService>();
            dto.CorpName = corpService.GetCorpInfo(entity.CorpId)?.Name ?? string.Empty;
            var projectService = applicationContext.GetService<ProjectService>();
            var projectInfo = projectService.GetProjectInfo(entity.ProjectId);
            if (projectInfo != null)
            {
                dto.ProjectName = projectInfo.Name;

                #region 主管单位数据权限

                if (applicationContext.CurrentUser is IDeptUser deptUser)
                {
                    if (deptUser.IsSuperAdministrator() || projectInfo.DeptId == deptUser.DeptId)
                        dto.HasEdit = true;
                    else dto.HasEdit = false;
                }

                #endregion 主管单位数据权限
            }
            return dto;
        }
    }
}